package com.google.code.puertorico.actors

import scala.actors.Actor
import com.google.code.puertorico._
import adt._

object TraderActor extends Actor {

  def act() {
    loop {
      react {
        case ActionSequence(state, actions) => {

          if (state.tradingHouseFull) {
            val newState = state.copy(activePlayer = state.nextActivePlayer); // TODO: need helper function
            EndTurnActor ! ActionSequence(newState, actions)
          } else {
            val tradingHouse = state.tradingHouse;

            if (!tradingHouse.hasCorn || hasOfficeWithStaff(state.activePlayer)) {
              val newState = sellCorn(state, state.activePlayer);
              NextPlayerActor ! ActionSequence(newState, SellCorn :: actions);
            }

            if (!tradingHouse.hasIndigo || hasOfficeWithStaff(state.activePlayer)) {
              val newState = sellIndigo(state, state.activePlayer);
              NextPlayerActor ! ActionSequence(newState, SellIndigo :: actions);
            }

            if (!tradingHouse.hasSugar || hasOfficeWithStaff(state.activePlayer)) {
              val newState = sellSugar(state, state.activePlayer);
              NextPlayerActor ! ActionSequence(newState, SellSugar :: actions);
            }

            if (!tradingHouse.hasTobacco || hasOfficeWithStaff(state.activePlayer)) {
              val newState = sellTobacco(state, state.activePlayer);
              NextPlayerActor ! ActionSequence(newState, SellTobbaco :: actions);
            }

            if (!tradingHouse.hasCoffee || hasOfficeWithStaff(state.activePlayer)) {
              val newState = sellCoffee(state, state.activePlayer);
              NextPlayerActor ! ActionSequence(newState, SellCoffee :: actions);
            }
          }
        }
      }
    }
  }

  // TODO: Rest

  def hasOfficeWithStaff(player: Player): Boolean = player.ownsOfficeBuilding && player.staffsOfficeBuilding;

  def sellCorn(state: BoardState, player: Player): BoardState = {
    //var newState = state.activePlayerTakeGold(sellingPrice(CornBarrel, player));

    // TODO: Actually sell corn
    throw new RuntimeException();
  }

  def sellIndigo(state: BoardState, player: Player): BoardState = throw new RuntimeException();

  def sellSugar(state: BoardState, player: Player): BoardState = throw new RuntimeException();

  def sellTobacco(state: BoardState, player: Player): BoardState = throw new RuntimeException();

  def sellCoffee(state: BoardState, player: Player): BoardState = throw new RuntimeException();

  def sellingPrice(barrel: Barrel, player: Player): Int = {
    // TODO: The player who chooses the trader may self for price + 1
    // TODO: Need save action choosen for each player

    var sellingPrice = barrel.price;
    if (player.staffsSmallMarket) {
      sellingPrice = sellingPrice + 1;
    }
    if (player.staffsLargeMarket) {
      sellingPrice = sellingPrice + 2;
    }
    sellingPrice;
  }

}